package org.sbbs.base.jsf.convertor;

import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.convert.FacesConverter;
import org.primefaces.convert.BooleanConverter;

@FacesConverter("org.sbbs.BooleanYesNo")
public class BooleanYesNoConverter extends BooleanConverter {


    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (context == null || component == null) {
            throw new NullPointerException();
        }

        // If the specified value is null, return a zero-length String
        if (value == null) {
            return "";
        }

        try {
            Boolean o = (Boolean) value;
            return o ? "是" : "否";
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (context == null || component == null) {
            throw new NullPointerException();
        }

        // If the specified value is null or zero-length, return null
        if (value == null) {
            return null;
        }
        value = value.trim();
        if (value.length() < 1) {
            return null;
        }

        // Let them know that the value being converted is not specifically
        // "true" or "false".
        try {
            Boolean o = Boolean.valueOf(value);
            return o ? "是" : "否";
        } catch (Exception e) {
            throw e;
        }
    }

}
